package com.example.myapplication.utils;

import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONObject;

import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtil {
    private static final String TAG = "OkHttpUtil";
    private static JSONObject response;
    private static Gson gson;
    private static List<String> ipList;


    public static JSONObject post(JSONObject jsonObject, String url) {
        ipList = IP.getIpList();
        MediaType type = MediaType.parse("application/json;charset=utf-8");
        RequestBody body = RequestBody.create(type, jsonObject.toString());

        OkHttpClient client = new OkHttpClient();
        for (int i = 0; i < ipList.size(); i++) {
            Log.d(TAG, "post: 当前ip地址:"+ipList.get(i));
            if (!ipList.get(i).isEmpty()) {
                try {
                    Request request = new Request.Builder()
                            .url(ipList.get(i) + url)
                            .post(body)
                            .build();

                    Response execute = client.newCall(request).execute();
                    response = new JSONObject(execute.body().string());
                    break;
                } catch (UnknownHostException | IllegalArgumentException e1) {
                    Log.d(TAG, "post: ip错误,尝试下一个ip");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                Log.d(TAG, "post: 错误,空ip");
            }
        }
        return response;
    }

    public static JSONObject post(Object object, String url) {
        ipList = IP.getIpList();
        gson = new Gson();
        String s = gson.toJson(object);

        MediaType type = MediaType.parse("application/json;charset=utf-8");
        RequestBody body = RequestBody.create(type, s);

        OkHttpClient client = new OkHttpClient();
        try {
            for (int i = 0; i < ipList.size(); i++) {
                try {
                    Request request = new Request.Builder()
                            .url(ipList.get(i) + url)
                            .post(body)
                            .build();

                    Response execute = client.newCall(request).execute();
                    response = new JSONObject(execute.body().string());
                    break;
                } catch (UnknownHostException | IllegalArgumentException | SocketTimeoutException e1) {
                    Log.d(TAG, "post: ip错误,尝试下一个ip");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (i == ipList.size() - 1) {
                    Log.d(TAG, "post: ip列表循环完成");
                }
            }
        } catch (NullPointerException e) {
            Log.d(TAG, "post: 没有ip地址可用");
        }
        return response;
    }

    public static JSONObject post(String url) {
        ipList = IP.getIpList();
        gson = new Gson();

        OkHttpClient client = new OkHttpClient();
        try {
            for (int i = 0; i < ipList.size(); i++) {
                try {
                    Request request = new Request.Builder()
                            .url(ipList.get(i) + url)
                            .build();

                    Response execute = client.newCall(request).execute();
                    response = new JSONObject(execute.body().string());
                    break;
                } catch (UnknownHostException | IllegalArgumentException | SocketTimeoutException e1) {
                    Log.d(TAG, "post: ip错误,尝试下一个ip");
                } catch (Exception e) {

                }
                if (i == ipList.size() - 1) {
                    Log.d(TAG, "post: ip列表循环完成");
                }
            }
        } catch (NullPointerException e) {
            Log.d(TAG, "post: 没有ip地址可用");
        }
        return response;
    }
}
